package com.example.yunhailutong.zhongtan.Adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.yunhailutong.zhongtan.R;
import com.example.yunhailutong.zhongtan.entity.InfoListEntity.DataBean;

import java.util.List;

public class CommonRecyclerViewAdapter extends RecyclerView.Adapter<CommonRecyclerViewAdapter.VH> implements View.OnClickListener{
    private List<DataBean> infoList;
    Context context = null;
    //声明自定义的监听接口
    private OnRecyclerviewItemClickListener mOnRecyclerviewItemClickListener = null;
    public CommonRecyclerViewAdapter(Context context, List<DataBean> data, OnRecyclerviewItemClickListener mOnRecyclerviewItemClickListener) {
        this.infoList = data;
        this.context = context;
        this.mOnRecyclerviewItemClickListener = mOnRecyclerviewItemClickListener;
    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_message_list_item, parent, false);
        v.setOnClickListener(this);
        return new VH(v);
    }

    @Override
    public void onBindViewHolder(@NonNull VH holder, final int position) {

        holder.message_list_item_title.setText(infoList.get(position).getIm_title());
        holder.message_list_item_time.setText(infoList.get(position).getIm_addTime());
        holder.itemView.setTag(position);
        if (infoList.get(position).getIm_imgPath() != null && !infoList.get(position).getIm_imgPath().isEmpty()) {
            Glide.with(context).load(infoList.get(position).getIm_imgPath()).into(holder.message_list_item_image);
        }
    }


    @Override
    public int getItemCount() {
        return infoList.size();
    }

    @Override
    public void onClick(View v) {
        //将监听传递给自定义接口
        mOnRecyclerviewItemClickListener.onItemClickListener(v, ((int) v.getTag()));
    }

    public static class VH extends RecyclerView.ViewHolder  {
        ImageView message_list_item_image;
        TextView message_list_item_title;
        TextView message_list_item_time;

        public VH(View itemView) {
            super(itemView);
            message_list_item_image = (ImageView) itemView.findViewById(R.id.message_list_view_item_iv);
            message_list_item_title = (TextView) itemView.findViewById(R.id.message_list_view_item_title);
            message_list_item_time = (TextView) itemView.findViewById(R.id.message_list_view_item_time);
        }
    }
    public interface OnRecyclerviewItemClickListener {
        void onItemClickListener(View v,int position);
    }

}
